package com.zm.tools.util.core.generatecode.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static com.zm.tools.util.core.generatecode.common.PackageNameUtil.getPackage;

/**
 * @author zoum
 * @create 2018/10/25 17:14
 */
public class GenerateManagerCode {

    private final static String MODEL_NAME = "manager";
    private final static Logger LOGGER = LoggerFactory.getLogger(GenerateManagerCode.class);

    public static void generate(String baseFilePath, String tableName, String companyName, String appName) throws IOException{
        String pakage = getPackage(companyName, appName, MODEL_NAME);
        generateManager4Write(baseFilePath, tableName, appName, MODEL_NAME, pakage);
        generateManager4Read(baseFilePath, tableName, appName, MODEL_NAME, pakage);
    }

    private static void generateManager4Write(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        generateManagerInterface4Write(baseFilePath, tableName, appName, modelName, pakage);
        generateManagerImpl4Write(baseFilePath, tableName, appName, modelName, pakage);
    }

    private static void generateManager4Read(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException {
        generateManagerInterface4Read(baseFilePath, tableName, appName, modelName, pakage);
        generateManagerImpl4Read(baseFilePath, tableName, appName, modelName, pakage);
    }

    private static void generateManagerInterface4Read(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateManagerInterface4Read start for " + tableName);
        String readOrWrite = "read";
        String parentPath = baseFilePath + "\\" + appName + "\\"+ appName + "-" + modelName +"\\src\\main\\java\\" + pakage.replace(".","\\") + "\\" + readOrWrite;
        File parentFile  = new File(parentPath);
        if (!parentFile.exists()){
            parentFile.mkdir();
        }
        String className = tableName + StringUtils.capitalize(readOrWrite) + "Manager.java";
        File classFile = new File(parentFile.getPath(), className);
        if (classFile.exists()){
            classFile.delete();
        }
        classFile.createNewFile();
        String voName = tableName + "VO";
        String dtoName = tableName + "DTO";
        String voInstanceName = String.valueOf(voName.charAt(0)).toLowerCase() + voName.substring(1);
        FileWriter fw = new FileWriter(classFile);
        StringBuilder sb = new StringBuilder();
        sb.append("package " + pakage + "."  + readOrWrite + ";\n\n")
                .append("import com.rongzhijia.member.model.vo."+ voName +";\n")
                .append("import com.rongzhijia.jiedianqian.basic.member.model.dto." + tableName.toLowerCase() + "." + dtoName + ";\n")
                .append("import com.rongzhijia.jdq.core.common.PageResult;\n")
                .append("import com.rongzhijia.jdq.core.orm.Pagination;\n")
                .append("import java.util.List;\n")
                .append("public interface " + className.split("\\.")[0] + " {\n\n")
                .append("   "+ voName +" get"+ tableName +"( " + voName + " " + voInstanceName + ");\n\n")
                .append("   List<"+ voName +"> query"+ tableName +"Service( " + voName + " " + voInstanceName + ", Pagination pagination);\n\n")
                .append("   PageResult<"+ voName +"> query"+ tableName +"PageService( " + voName + " " + voInstanceName + ", Pagination pagination);\n\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateManagerInterface4Read end for " + tableName);
    }

    private static void generateManagerImpl4Read(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateManagerImpl4Read start for " + tableName);
        String readOrWrite = "read";
        String parentPath = baseFilePath + "\\" + appName + "\\"+ appName + "-" + modelName +"\\src\\main\\java\\" + pakage.replace(".","\\") + "\\" + readOrWrite + "\\impl";
        File parentFile  = new File(parentPath);
        if (!parentFile.exists()){
            parentFile.mkdir();
        }
        String className = tableName + StringUtils.capitalize(readOrWrite) + "ManagerImpl.java";
        File classFile = new File(parentFile.getPath(), className);
        if (classFile.exists()){
            classFile.delete();
        }
        classFile.createNewFile();
        String dtoName = tableName + "DTO";
        String voName = tableName + "VO";
        String convertName = tableName + "Convert";
        String interfaceName = tableName + StringUtils.capitalize(readOrWrite) + "Manager";
        String voInstanceName = String.valueOf(voName.charAt(0)).toLowerCase() + voName.substring(1);
        String facadeName = tableName + StringUtils.capitalize(readOrWrite) + "Facade";
        String facadeInstanceName = String.valueOf(facadeName.charAt(0)).toLowerCase() + facadeName.substring(1);
        FileWriter fw = new FileWriter(classFile);
        StringBuilder sb = new StringBuilder();
        sb.append("package " + pakage + "." + readOrWrite + ".impl;\n\n")
                .append("import com.rongzhijia.member.model.vo."+ voName +";\n")
                .append("import com.rongzhijia.jiedianqian.basic.member.model.dto." + tableName.toLowerCase() + "." + dtoName + ";\n")
                .append("import com.rongzhijia.member.manager."+ readOrWrite + "." + interfaceName +";\n")
                .append("import com.rongzhijia.member.model.convert."+ convertName +";\n")
                .append("import com.rongzhijia.member.facade." + readOrWrite + "."+ facadeName + ";\n\n")
                .append("import org.springframework.beans.factory.annotation.Autowired;\n")
                .append("import com.rongzhijia.jdq.core.common.PageResult;\n")
                .append("import com.rongzhijia.jdq.core.orm.Pagination;\n")
                .append("import org.springframework.stereotype.Service;\n")
                .append("import java.util.List;\n\n")
                .append("@Service\n")
                .append("public class " + className.split("\\.")[0] + " implements "+ interfaceName + " {\n\n")
                .append("   @Autowired\n")
                .append("   private " + facadeName + " " + facadeInstanceName + ";\n\n")
                .append("   @Override\n")
                .append("   public "+ voName +" get"+ tableName +"( " + voName + " " + voInstanceName + "){\n")
                .append("       return "+convertName+".toVO(" + facadeInstanceName + ".get" + tableName + "Service("+ convertName +".toDTO("+ voInstanceName +")));\n")
                .append("   }\n\n")
                .append("   @Override\n")
                .append("   public List<"+ voName +"> query"+ tableName +"Service( " + voName + " " + voInstanceName + ", Pagination pagination){\n")
                .append("       return "+convertName+".toVOS(" + facadeInstanceName + ".query" + tableName + "Service("+ convertName +".toDTO("+ voInstanceName +"), pagination));\n")
                .append("   }\n\n")
                .append("   @Override\n")
                .append("   public PageResult<"+ voName +"> query"+ tableName +"PageService( " + voName + " " + voInstanceName + ", Pagination pagination){\n")
                .append("       PageResult<"+ dtoName +"> pageResult = "+ facadeInstanceName + ".select" + tableName + "PageService("+ convertName +".toDTO("+ voInstanceName +"), pagination);\n")
                .append("       PageResult<"+ voName +"> voPageResult = new PageResult<>();\n")
                .append("       voPageResult.copy(pageResult);\n")
                .append("       voPageResult.setList("+ convertName +".toVOS(pageResult.getList()));\n")
                .append("       return voPageResult;\n")
                .append("   }\n\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateManagerImpl4Read end for " + tableName);
    }

    private static void generateManagerInterface4Write(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateManagerInterface4Write start for " + tableName);
        String readOrWrite = "write";
        String parentPath = baseFilePath + "\\" + appName + "\\"+ appName + "-" + "\\src\\main\\java\\" + pakage.replace(".","\\") + "\\" + readOrWrite;
        File parentFile  = new File(parentPath);
        if (!parentFile.exists()){
            parentFile.mkdir();
        }
        String className = tableName + StringUtils.capitalize(readOrWrite) + "Manager.java";
        File classFile = new File(parentFile.getPath(), className);
        if (classFile.exists()){
            classFile.delete();
        }
        classFile.createNewFile();
        String voName = tableName + "VO";
        String voInstanceName = String.valueOf(voName.charAt(0)).toLowerCase() + voName.substring(1);
        FileWriter fw = new FileWriter(classFile);
        StringBuilder sb = new StringBuilder();
        sb.append("package " + pakage + "."  + readOrWrite + ";\n\n")
                .append("import com.rongzhijia.member.model.vo."+ voName +";\n")
                .append("public interface " + className.split("\\.")[0] + " {\n\n")
                .append("   int add( " + voName + " " + voInstanceName + ");\n\n")
                .append("   int update( " + voName + " " + voInstanceName + ");\n\n")
                .append("   int delete( " + voName + " " + voInstanceName + ");\n\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateManagerInterface4Write end for " + tableName);
    }

    private static void generateManagerImpl4Write(String baseFilePath, String tableName, String appName, String modelName, String pakage) throws IOException{
        LOGGER.info("generateManagerImpl4Write start for " + tableName);
        String readOrWrite = "write";
        String parentPath = baseFilePath + "\\" + appName + "\\"+ appName + "-" + modelName + "\\src\\main\\java\\" + pakage.replace(".","\\") + "\\" + readOrWrite + "\\impl";
        File parentFile  = new File(parentPath);
        if (!parentFile.exists()){
            parentFile.mkdir();
        }
        String className = tableName + StringUtils.capitalize(readOrWrite) + "ManagerImpl.java";
        File classFile = new File(parentFile.getPath(), className);
        if (classFile.exists()){
            classFile.delete();
        }
        classFile.createNewFile();
        String dtoName = tableName + "DTO";
        String voName = tableName + "VO";
        String convertName = tableName + "Convert";
        String interfaceName = tableName + StringUtils.capitalize(readOrWrite) + "Manager";
        String voInstanceName = String.valueOf(voName.charAt(0)).toLowerCase() + voName.substring(1);
        String facadeName = tableName + StringUtils.capitalize(readOrWrite) + "Facade";
        String facadeInstanceName = String.valueOf(facadeName.charAt(0)).toLowerCase() + facadeName.substring(1);
        FileWriter fw = new FileWriter(classFile);
        StringBuilder sb = new StringBuilder();
        sb.append("package " + pakage + "."  + readOrWrite + ".impl;\n\n")
                .append("import com.rongzhijia.member.manager."+ readOrWrite + "." + interfaceName +";\n")
                .append("import com.rongzhijia.member.model.vo."+ voName +";\n")
                .append("import com.rongzhijia.member.model.convert."+ convertName +";\n")
                .append("import com.rongzhijia.member.facade." + readOrWrite + "."+ facadeName + ";\n\n")
                .append("import org.springframework.beans.factory.annotation.Autowired;\n")
                .append("import org.springframework.stereotype.Service;\n\n")
                .append("import java.util.Date;\n\n")
                .append("@Service\n")
                .append("public class " + className.split("\\.")[0] + " implements "+ interfaceName +"{\n\n")
                .append("   @Autowired\n")
                .append("   private " + facadeName + " " + facadeInstanceName + ";\n\n")
                .append("   @Override\n")
                .append("   public int add( " + voName + " " + voInstanceName + "){\n")
                .append("       return " + facadeInstanceName + ".add(" + convertName + ".toDTO(" + voInstanceName + "));\n")
                .append("   }\n\n")
                .append("   @Override\n")
                .append("   public int update( " + voName + " " + voInstanceName + "){\n")
                .append("       return " + facadeInstanceName + ".update(" + convertName + ".toDTO(" + voInstanceName + "));\n")
                .append("   }\n\n")
                .append("   @Override\n")
                .append("   public int delete( " + voName + " " + voInstanceName + "){\n")
                .append("       return " + facadeInstanceName + ".delete(" + convertName + ".toDTO(" + voInstanceName + "));\n")
                .append("   }\n")
                .append("}");
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generateManagerImpl4Write end for " + tableName);
    }

}
